/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import entity.Lists;
import entity.Users;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import services.implementation.ForHelp;
import services.implementation.ListsService;
import services.implementation.UsersService;

/**
 *
 * @author smile
 */
@ManagedBean(name = "listController")
@SessionScoped
public class ListsController {
    @EJB
    private UsersService usersService;
    @EJB
    private ListsService listsService;

    private Integer _id;
    private String _name;
    private Users _idUser; 
    private int _current_list_id;

    public Integer getId() {
        return _id;
    }

    public void setId(Integer _id) {
        this._id = _id;
    }

    public Users getIdUser() {
        return _idUser;
    }

    public void setIdUser(Users _idUser) {
        this._idUser = _idUser;
    }

    public String getName() {
        return _name;
    }

    public void setName(String _name) {
        this._name = _name;
    }
    
    public int getCurrent_list_id() {
        return _current_list_id;
    }

    public void setCurrent_list_id(int _current_list_id) {
        this._current_list_id = _current_list_id;
    }

    /** Creates a new instance of ListsController */
    public ListsController() {
    }
    
    public List<Lists> getAllLists (){
        return listsService.getAllLists();
    }
    
    /*
     * Add new List.
     */
    public String create() {
        try{
            FacesContext context = FacesContext.getCurrentInstance();
            Lists list = new Lists();
            list.setId(1);
            list.setName(this._name);
            // find user by his email
            List<Users> user = usersService.findByEmail(
                    context.getExternalContext().getUserPrincipal().getName());
            list.setIdUser(user.get(0));
            listsService.addList(list);
            return "List"; 
        }catch(Exception e){
            ForHelp.addErrorMessage(
                    e, 
                    ResourceBundle
                    .getBundle("/Bundle")
                    .getString("Lists.Add.Error"));
            return null;
        }
    }

    /*
     * Checks whether the user lists. If user has got lists return 1
     */
    public Integer isListsExist(){
        try{
            FacesContext context = FacesContext.getCurrentInstance();
            // find user by his email
            List<Users> user = usersService.findByEmail(
                    context.getExternalContext().getUserPrincipal().getName());
            // get lists by user. if lists is empty return 0
            if (listsService.getListByUserId(user.get(0)).isEmpty()){
                return 0;
            }
            return 1;
        }catch(Exception e){
            ForHelp.addErrorMessage(e, e.getMessage());
            return 1;
        }
    }
    
    /*
     * Get all lists by user.
     */
    public List<Lists> getListsByUser (){    
        try{
            FacesContext context = FacesContext.getCurrentInstance();
            List<Users> user = usersService.findByEmail(
                    context.getExternalContext().getUserPrincipal().getName());
            return listsService.getListByUserId(user.get(0));        
        } catch (Exception e){
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }
        
    } 
    
    /*
     * Edit list in database.
     */
    public String edit() {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            List<Users> user = usersService.findByEmail(
                    context.getExternalContext().getUserPrincipal().getName());
            Lists list = new Lists();
            list = listsService.getListById(this._current_list_id);
            this._id = list.getId();
            this._name = list.getName();
            this._idUser = user.get(0); 
            return "Edit_list";
        } catch (Exception e) {
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }
    }
    
    /*
     * Delete list from database.
     */
    public String destroy() {
        try {
            Lists lists = new Lists();
            lists = listsService.getListById(this._current_list_id);
            listsService.deleteList(lists);
//            ForHelp.addSuccessMessage(
//                    ResourceBundle
//                    .getBundle("/Bundle")
//                    .getString("List.Deleted")
//            );
            return "List";
        } catch (Exception e) {
            ForHelp.addErrorMessage(
                    e, 
                    ResourceBundle
                    .getBundle("/Bundle")
                    .getString("Persistence.Error.Occured")
            );
            return null;
        }
    }
    
    /*
     * Update list in database.
     */
    public String update(){
        try {          
            Lists list = new Lists();
            list.setId(this._current_list_id);
            list.setName(this._name);
            list.setIdUser(this._idUser);
            listsService.updateList(list);
            return "List";
        } catch (Exception e) {
            ForHelp.addErrorMessage(e, e.getMessage());
            return null;
        }
    }
}